/**
 * Created by wuyu on 2016/8/19.
 */
var indexApp = angular.module("indexApp", ['ngRoute', 'ngResource']);
indexApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/dynamic/host', {controller: 'hostController', templateUrl: '/admin/host.html'})
        .when('/dynamic/deploy', {controller: 'deployController', templateUrl: '/admin/deploy.html'})
        .when('/dynamic/undeploy', {controller: 'undeployController', templateUrl: '/admin/undeploy.html'})
        .when('/dynamic/jar', {controller: 'jarController', templateUrl: '/admin/jar.html'})
        .otherwise({controller: 'hostController', templateUrl: '/admin/host.html'});
}]);

indexApp.service("jsonService", function () {
    this.remove = function (data, id) {
        var index = this.findIndex(data, id);
        if (index !== -1) {
            data.splice(index, 1);
        }
    }

    this.findIndex = function (data, id) {
        var index = -1;

        angular.forEach(data, function (item, key) {
            if (item.id === id) {
                index = key;
                return;
            }
        });
        return index;
    }
})

indexApp.controller("hostController", function ($scope, $http) {
    $http.get("/dynamic/listDynamicService")
        .success(function (response) {
                $scope.data = response;
            }
        );

})

indexApp.controller("undeployController", function ($scope, $http, jsonService) {
    $scope.list = function () {
        $http.get("/dynamic/listUnDynamicService")
            .success(function (response) {
                $scope.data = response;
            });
    }

    $scope.list();

    $scope.addOrUpdateModel = function (id) {
        if (id != 0) {
            $http.get("/dynamic/" + id)
                .success(function (response) {
                    $scope.serviceConfig = response;
                    $scope.addServiceModal()
                })
        } else {
            $scope.serviceConfig = {};
            $scope.addServiceModal()
        }
    }

    $scope.addOrUpdate = function (id) {
        if (id) {
            $scope.update();
        } else {
            $scope.addService()
        }
    }

    $scope.addService = function () {
        $http.post("/dynamic/", $scope.serviceConfig, jsonService)
            .success(function (response) {
                $scope.data[$scope.data.length] = $scope.serviceConfig;
                layer.msg('添加' + $scope.serviceConfig.id + '成功!');
            })
            .error(function (response) {
                //正上方
                layer.msg("添加失败!" + response.message, {
                    offset: 0,
                    shift: 6
                });
            })

    }

    $scope.update = function () {
        $http.put("/dynamic/", $scope.serviceConfig)
            .success(function (response) {
                var srviceConfigIndex = jsonService.findIndex($scope.data, $scope.serviceConfig.id);
                $scope.data[srviceConfigIndex] = $scope.serviceConfig;
                layer.msg("更新成功!")
            })
            .error(function (response) {
                layer.msg(response.message);
            })

    }

    $scope.deleteService = function (id) {
        $http.delete("/dynamic/" + id)
            .success(function (response) {
                jsonService.remove($scope.data, id);
                layer.msg('删除成功!');
            })
            .error(function (response) {
                layer.msg("删除失败," + response.message);
            })
    }

    $scope.export = function (id) {
        $http.get("/dynamic/export/" + id)
            .success(function (response) {
                layer.msg("发布成功!")
            })
            .error(function (response) {
                layer.msg(response.message);
            })
    }

    $scope.addServiceModal = function () {
        $('#addServiceModal').modal('toggle')
    }
})


indexApp.controller("deployController", function ($scope, $http, jsonService) {


    $http.get("/dynamic/listDynamicBean")
        .success(function (response) {
                $scope.data = response;
            }
        );


    $scope.unexport = function (id, url) {
        $http.get("/dynamic/unexport?id=" + id + "&url=" + url)
            .success(function (response) {
                jsonService.remove($scope.data, id);
            })
    }


})

indexApp.controller("jarController", function ($scope, $http) {
    $http.get("/jar/listJars")
        .success(function (response) {
                $scope.data = response;
            }
        );

    $scope.scanJar = function (name) {
        $http.get("/jar/scanJar?jar=" + name)
            .success(function (response) {
                $http.post("/dynamic/insertBatch", response)
                    .error(function (response) {
                        layer.msg(response.exception + ":" + response.message);
                    })
                layer.msg("扫描完成,请到未发布服务中查看!")
            })
            .error(function (response) {
                layer.msg(response.exception + ":" + response.message);
            })
    }
    $scope.toUpload = function () {
//弹出即全屏
        var index = layer.open({
            type: 2,
            title: "上传Jar包",
            content: '/upload/upload.html',
            area: ['800px', '640px'],
            maxmin: true
        });
        // layer.full(index);
    }
})